package com.wormpex.data.db;

import com.wormpex.api.json.JsonUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * User: shihai.zhang
 * Date: 17-7-6
 * Time: 上午8:47
 * DESC:
 */

@MappedTypes(List.class)
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ListStringTypeHandler extends BaseTypeHandler<List<String>> {

    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        String s = JsonUtil.of(strings);
        preparedStatement.setString(i, s);
    }

    public List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String stringStr = resultSet.getString(s);
        return JsonUtil.of(stringStr, List.class);
    }

    public List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String s = resultSet.getString(i);
        return JsonUtil.of(s, List.class);
    }

    public List<String> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String s = callableStatement.getString(i);
        return JsonUtil.of(s, List.class);
    }
}